// JavaScript Document

var gatherURL = "gathersurvey.py";

function gather_prefetch() {

	$.mobile.loadPage( "info.html" );
	$.mobile.loadPage( "gatherFall.html" );
	$.mobile.loadPage( "gatherBasket.html" );
}


function sendGather() {
	
	var url = urlThrive + gatherURL;
	var args = $('#gather_form').serialize();
	args = args + "&user_name=" + USER_NAME;
	args = args + "&device_id=" + TEST_DEVICE;
	args = args + "&survey_date=" + getTimeStamp();
	
	if(DEBUG_ALERTS) {
		alert(url + "?" + args);
	}
	
	removePopup();
	$.mobile.silentScroll(0);
	showPopup('&nbsp;&nbsp;Sending...&nbsp;&nbsp;');
	
	jQuery.ajax({
		timeout: 8000,
		type: "GET",
		url: url,
		data: args,
		dataType: "text",
		crossDomain: true,

		//success: function (msg) {
		success: function(data, textStatus, jqXHR){

			var message = data.split(":");
			
			if( message.length < 3) {
				$.mobile.silentScroll(0);
				removePopup();
				showPopup('Server error, please try again later.'); 
				return;
			}
			
			var error = parseInt(message[0]);
			
			// success! redirect to login page
			if (error == 0){
				removePopup();
				$.mobile.changePage("gatherFall.html");	
			}
			
			// error! display message
			else if (error == -1) {
				$.mobile.silentScroll(0);
				removePopup();
				showPopup(message[1]); 
			}
			
			// error! display message and move to form element
			else if (error == -2 ) {
				$.mobile.silentScroll(0);
				removePopup();
				showPopup(message[1]); 
			}
		},
		error: function(jqXHR, textStatus, errorThrown){

			removePopup();
			$.mobile.changePage("gatherFall.html");
		}
	}); 
}